<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang==='zh' ? '400px':'420px'" @close='close' top="20vh">
    <div class="dialogContentBox">
      <el-form ref="form" :model="record" :label-width="lang==='zh' ?'60px':'110px'" label-position="left" :rules="rules" style="margin:0 20px;">
        <el-form-item :label="$t('接口名称')" prop="interface">
          <el-select v-model="record.interface" ref="interface" filterable allow-create default-first-option clearable size='small' :placeholder="$t('请选择')" style="width:200px">
            <el-option v-for="item in interfaceList" :disabled="item.enPortMode === 3" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('源地址')" prop="source_ip">
          <el-input v-emoji v-model="record.source_ip" ref="sourceIp" style="width:200px" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('组播IP')" prop="group_ip">
          <el-input v-emoji v-model="record.group_ip" ref="groupIp" style="width:200px" size='small'></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addGroupMulticast } from '@/api/network/route/multicast'
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'MulticastDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }

    return {
      lang: this.$i18n.locale,
      dialogVisible: false,
      record: {
        interface: '',
        source_ip: '',
        group_ip: ''
      },
      recordCopy: {
        interface: '',
        source_ip: '',
        group_ip: ''
      },
      interfaceList: [],
      rules: {
        interface: [{ required: false, validator: validateName, trigger: 'blur' }],
        source_ip: [{ required: false, validator: validateName, trigger: 'blur' }],
        group_ip: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['sourceIp', 'groupIp']
        const selectData = ['interface']
        selectClass(this.$refs, selectData)
        inputClass(this.$refs, data)
      })
      getPhysicalApiArray().then(res => { // strPhysicsName
        if (res.status === 0) {
          this.interfaceList = res.info
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addGroupMulticast(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
      this.interfaceList = []
    }
  }
}
</script>
<style scoped>
.dialogContentBox {
  max-height: calc(60vh - 184px);
  overflow-y: auto;
}
</style>
